package yur.wedding.modules.sys.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import yur.wedding.common.utils.Constant;
import yur.wedding.common.utils.Query;

import yur.wedding.modules.sys.dao.SysMenuDao;
import yur.wedding.modules.sys.entity.SysMenuEntity;
import yur.wedding.modules.sys.service.SysMenuService;
import yur.wedding.modules.sys.service.SysRoleMenuService;
import yur.wedding.modules.sys.service.SysUserRoleService;
import yur.wedding.modules.sys.service.SysUserService;
import yur.wedding.utils.PageUtils;

import javax.annotation.Resource;


@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {


    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysMenuEntity> page = this.page(
                new Query<SysMenuEntity>().getPage(params),
                new QueryWrapper<SysMenuEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 导航栏
     *
     * @param userId
     * @return
     */
    @Override
    public List<SysMenuEntity> getUserMenuList(Long userId) {

        //判断是否最高管理员
        if (userId == Constant.SUPER_ADMIN) {
            //查询所以目录权限
            return getAllMenuList(null);
        } else {

            List<Long> menulist = new ArrayList();//保存数据
            //获取用户角色对应的角色
            List<Long> rolelist = sysUserRoleService.queryRoleIdList(userId);
            for (Long roleid : rolelist) {
                //通过角色ID 查询菜单权限
                List<Long> doorIdList = sysRoleMenuService.queryMenuIdList(roleid);
                for (Long doorid : doorIdList) {
                    menulist.add(doorid);
                }
            }
            //获取所以菜单信息   menulist 为用户菜单
            List<SysMenuEntity> allMenuList = getAllMenuList(menulist);
            return allMenuList;
        }
    }

    /**
     * 获取所以菜单信息
     *
     * @param menuIdList
     * @return
     */
    private List<SysMenuEntity> getAllMenuList(List<Long> menuIdList) {
        //1  查询根目录菜单  根目录为0
        List<SysMenuEntity> menulist = queryListParentId(0L, menuIdList);


        //2  查询子目录菜单
        getMenuTreeList(menulist, menuIdList);
        return menulist;
    }


    /**
     * 根据父类菜单  查子菜单
     *
     * @param i          目录等级id   根目录为0
     * @param menuIdList
     * @return
     */
    @Override
    public List<SysMenuEntity> queryListParentId(Long i, List<Long> menuIdList) {
        List<SysMenuEntity> menulist = queryListParentId(i);
        //没有用户菜单代表   管理员直接返回查询菜单即可
        if (menuIdList == null) {
            return menulist;
        }
        //用户菜单
        List<SysMenuEntity> userMenulist = new ArrayList();
        for (SysMenuEntity menu : menulist) {
            //判断用户菜单 必须有一个才会执行
            if (menuIdList.contains(menu.getMenuId())) {
                userMenulist.add(menu);
            }
        }
        return userMenulist;
    }

    /**
     * 根据父类菜单  查子菜单
     *
     * @param i 目录等级id   根目录为0
     * @return
     */
    @Override
    public List<SysMenuEntity> queryListParentId(Long i) {

        return baseMapper.queryListParentId(i);
    }

    @Override
    public List<SysMenuEntity> queryNotButtonList() {

        return baseMapper.queryNotButtonList();
    }

    /**
     * 递归查询子菜单
     *
     * @param menulist   上级菜单id
     * @param menuIdList
     */
    private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menulist, List<Long> menuIdList) {

        List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();
        //遍历上级菜单
        for (SysMenuEntity emnu : menulist) {
            //判断菜单id  等于目录类型 Constant  静态类
            if (emnu.getType() == Constant.MenuType.CATALOG.getValue()) {

                emnu.setList(getMenuTreeList(queryListParentId(emnu.getMenuId(), menuIdList), menuIdList));
                System.out.println("子菜单查询中的递归数据" + emnu.getList());

            }
            subMenuList.add(emnu);
        }

        return subMenuList;
    }
}
